<?php

class SnippetController extends Zend_Controller_Action {

    public function preDispatch() {
        $t = Zend_Registry::get('Zend_Locale');
        $this->view->navigation_line = array($t->_('Snippets') => ADMIN_URL . '/snippet');
        $session = new Zend_Session_Namespace('UnCMS');
        if (!isset($session->acl->snippet))
            $this->_redirect(SITE_URL);
    }

    public function indexAction() {
        $model = new SnippetModel();
        $this->view->list = $model->getSnippets();
        $model = new PageModel();
        $this->view->mods = $model->getAddonsList('page');
        $this->view->modules = $model->getAddonsList('page', 'title');
    }

    public function editAction() {
        $formmodel = new SnippetForm();
        $sid = (int) $this->getRequest()->getParam('id', 0);
        $form = $formmodel->Snippet($sid);
        $model = new SnippetModel();
        $t = Zend_Registry::get('Zend_Locale');
        if ($sid > 0) {
            $snip = $model->getSnippets($sid);
            $this->view->snip = $snip;
            $this->view->navigation_line = $this->view->navigation_line + array($snip['title'] => ADMIN_URL . '/snippet/edit/id/' . $sid);
        } else {
            $this->view->navigation_line = $this->view->navigation_line + array($t->_('New snippet') => ADMIN_URL . '/snippet/edit/id/' . $sid);
        }

        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                $formData = $this->_request->getPost();
                if ($sid > 0)
                    unlink(APP_PATH . '/templates/snippets/' . $snip['file'] . '.phtml');
                $file = fopen(APP_PATH . '/templates/snippets/' . $formData['file'] . '.phtml', 'w+');
                fwrite($file, $formData['editarea']);
                fclose($file);
                $sid = $model->updateSnippet(array('file' => $formData['file'], 'title' => $formData['title'], 'mid' => $formData['mid']), $sid);
                $this->_redirect('/snippet/edit/id/' . $sid);
            } else {
                $form->populate($formData);
            }
        } else {
            if ($sid > 0) {
                if (file_exists(APP_PATH . '/templates/snippets/' . $snip['file'] . '.phtml')) {
                    $code = file_get_contents(APP_PATH . '/templates/snippets/' . $snip['file'] . '.phtml');
                } else {
                    $code = '';
                }
                $form->populate(array('editarea' => $code, 'file' => $snip['file'], 'title' => $snip['title'], 'mid' => $snip['mid']));
            }
        }
        $this->view->form = $form;
    }

    public function deleteAction() {
        $sid = (int) $this->getRequest()->getParam('id', 0);
        $model = new SnippetModel();
        $snip = $model->getSnippets($sid);
        if (file_exists(APP_PATH . '/templates/snippets/' . $snip['file'] . '.phtml'))
            unlink(APP_PATH . '/templates/snippets/' . $snip['file'] . '.phtml');
        $model->deleteSnippet($sid);
        $this->_forward('index');
    }

}
